from odoo import api, fields, tools, models, _


class UoMCategoryExt(models.Model):
    _inherit = 'uom.category'
    category_type = fields.Selection([
        ('base', 'Base'), ('prod', 'Production'), ('pur', 'Procurement'), ('stock', 'Inventory')
    ], string='Category Type', default='base', required=True)


class UomUomExt(models.Model):
    _inherit = 'uom.uom'

    uom_us = fields.Char(string='English Name')

    display_name = fields.Char(compute='_compute_display_name', string='Display Name')
    record_name = fields.Char(string='Project Record')
    uom_name_id = fields.Many2one('uom.name', string='Uom Name')

    @api.depends('name', 'uom_us')
    def _compute_display_name(self):
        for rec in self:
            if rec.name and rec.uom_us and rec.name != rec.uom_us:
                rec.display_name = rec.name + '【' + rec.uom_us + '】'
            else:
                rec.display_name = rec.name

    @api.onchange('name')
    def onchange_record_name(self):
        self.record_name = self.get_record_name(self.name)

    def cron_update_record_name(self):
        objs = self.search([])
        for obj in objs:
            obj.record_name = self.get_record_name(obj.name)

    def get_record_name(self, name):
        records = {
            'SHETL': 'SheetG',
            'SHETW': 'SheetF',
            'SHEET': 'PNL/Sheet',
            'PCSPS': 'PCS/Sheet',
            'PNLL': 'PNLX',
            'PNLW': 'PNLY',
            'SetL': 'SETX',
            'SetW': 'SETY',
            'PCSL': 'PCSX',
            'PCSW': 'PCSY',
            'PANEL': 'PCS/PNL',
            'SET': 'PCS/SET',
        }
        return records.get(name, name)

    def cron_update_uom_name_id(self):
        square_mset = self.env.ref("pcbmanufact.uom_name_square_mset").id
        square_mpcs = self.env.ref("pcbmanufact.uom_name_square_mpcs").id
        square_pnl = self.env.ref("pcbmanufact.uom_name_square_pnl").id
        mm = self.env.ref("pcbmanufact.uom_name_mm").id
        pnl_sheet = self.env.ref("pcbmanufact.uom_name_pnl_sheet").id
        pcs_sheet = self.env.ref("pcbmanufact.uom_name_pcs_sheet").id
        pcs_pnl = self.env.ref("pcbmanufact.uom_name_pcs_pnl").id
        pcs_set = self.env.ref("pcbmanufact.uom_name_pcs_set").id
        objs = self.search([("category_id", '=', 7)])
        for i, obj in enumerate(objs):
            if obj.name == 'SETA':
                obj.uom_name_id = square_mset
            elif obj.name == 'PCSA':
                obj.uom_name_id = square_mpcs
            elif obj.name == 'PNLA':
                obj.uom_name_id = square_pnl
            elif obj.name in ['SHETL', 'SHETW', 'PNLL', 'PNLW', 'SetL', 'SetW',
                                     'PCSL', 'PCSW', 'SETL', 'SETW', 'PCS Width', 'PCS Length',
                                     'Panel Width', 'Panel Length', 'Set Width', 'Set Length']:
                obj.uom_name_id = mm
            elif obj.name == 'SHEET':
                obj.uom_name_id = pnl_sheet
            elif obj.name == 'PCSPS':
                obj.uom_name_id = pcs_sheet
            elif obj.name == 'PANEL':
                obj.uom_name_id = pcs_pnl
            elif obj.name == 'SET':
                obj.uom_name_id = pcs_set
            else:
                pass
